package se581.banker.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * This class generates data for the simulations
 * 
 * @author mmichalak
 *
 */
public class DataGenerator {

		
	public static void main(String[] args) {
		
		int NUM_TICKS = 100;
		int MAX_NUM_EVENT_FOR_TICK = 10;
		int MIN_ID = 10000; int MAX_ID = 11000;
		boolean genBlankEvents = true;  // No blank ticks.  Every tick will have Event
		String outputFileName = "event_data.txt";
		
		ArrayList<String> lines = new ArrayList<String>();
		Map<String, String> idMap = new HashMap<String,String>();

		int NUM_TRANS_TYPE = 3; // TIed to getTransType() method.  Generally won't chg		
		int tick = 1;
		int id = -1;
		String transType = "";
		int transAmt = 0;
		int numEventsForTick = 15;
		OutputFileWriterUtil filewriter = new OutputFileWriterUtil(outputFileName);

		DataGenerator dataGen = new DataGenerator();
		Random generator = new Random();

		

		while (tick < NUM_TICKS) {
			numEventsForTick = generator.nextInt(MAX_NUM_EVENT_FOR_TICK);
			boolean writeLine = true;
			do {
				id = MIN_ID + (int)(Math.random() * ((MAX_ID - MIN_ID) + 1));
	
				// First time for id, put in Map and force a NEW event
				if (!idMap.containsKey(Integer.toString(id))) {
					transType = "NEW";
					idMap.put(Integer.toString(id), Integer.toString(id));
					transAmt = generator.nextInt(5000);
				}
				else {
					transType = dataGen.getTransType( generator.nextInt(NUM_TRANS_TYPE) ); // Align to vals in getTransType() 
				}
				if (transType.equals("BLANK") && !genBlankEvents) {
					writeLine = false;
					continue;
				}
				
				StringBuilder line = new StringBuilder();
				line.append(tick).append(" ").append(transType).append(" ");
				if ("UPDATE".equalsIgnoreCase(transType) || "NEW".equalsIgnoreCase(transType)) {
					transAmt = generator.nextInt(5000) - generator.nextInt(5000);
					line.append(id).append(" ").append(transAmt);
				}
				else {
					line.append(id);
				}
				//lines.add(line.toString());
				filewriter.writeToFileWOBlanks(line.toString());
			} while ( (numEventsForTick-- - 1) >= 0);

			if (!writeLine)
				continue; // Used to skip outputting BLANK lines
			
			tick++;
		}

		//NOTE adding a few blank ticks at end so that threads can finish before prog stops
		StringBuilder blank = new StringBuilder();  	blank.append(tick++).append(" ").append(dataGen.getTransType(2)).append(" 100"); 
			filewriter.writeToFileWOBlanks(blank.toString());
		blank = new StringBuilder();  	blank.append(tick++).append(" ").append(dataGen.getTransType(2)).append(" 100");
			filewriter.writeToFileWOBlanks(blank.toString());
		blank = new StringBuilder();  	blank.append(tick++).append(" ").append(dataGen.getTransType(2)).append(" 100");
			filewriter.writeToFileWOBlanks(blank.toString());
		
		//System.out.println(lines);
	}
	
	
	public String getTransType(int rand) {
		String transType = null;
		switch (rand) {
			case 0:
				transType = "UPDATE";
				break;
			case 1:
				transType = "QUERY BALANCE";
				break;
			case 2:
				transType = "BLANK";
				break;
		}
		
		return transType;
	}
	

}
